package cl.whyem.testsutilityproject;

import cl.whyem.testsutilityproject.guava.GuavaFluentIterable;
import cl.whyem.testsutilityproject.guava.dto.BaseDTO;
import cl.whyem.testsutilityproject.guava.functions.MyReplaceFrom;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

/**
 * Test de cosas varias, libs, pruebas de concepto Ejercicios para fortalecer
 * conocimientos.
 *
 * @author Yamil Medina C.
 *
 */
public class App {

    public static void main(String[] args) {
//        List<Type> dynamicList = fillDataDynamic();
//        for (Type t : dynamicList) {
//            System.out.println("T -> " + t.getDynamicValue(t));
//        }
        optionaler(null, null);
    }

    public static void optionaler(Long id, String gearbox) {
        //        String _id = Strings.nullToEmpty(String.valueOf(id));
        //        String _gearbox = Strings.nullToEmpty(String.valueOf(gearbox));
        //        String _idOpt = Strings.nullToEmpty(String.valueOf(Optional.of(id)));
        
//        new Optional<T>(){
//        
//        }
        String _idOpt = Strings.nullToEmpty(Objects.firstNonNull(id, "").toString());
        String _gearboxOpt = Strings.nullToEmpty(Objects.firstNonNull(gearbox, "").toString());

//        System.out.println("id -> " + _id);
//        System.out.println("gearbox -> " + _gearbox);

        System.out.println("id -> " + _idOpt);
        System.out.println("gearbox -> " + _gearboxOpt);
        
        
    }

    public static List<Type> fillDataDynamic() {
        return Arrays.asList(new Type[]{new Type(1), new Type(2), new Type("dog")});
    }

    public static void diffs() {
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();

        list1.add("sultan");
        list1.add("Masum");
        list1.add("sultan");
        list1.add("Sorry");
        list1.add("sultan");
        list1.add("Masum");
        list1.add("sultan");
        list1.add("Tarek");
        list1.add("sultan");

        list2.add("Billal");
        list2.add("Masum");
        list2.add("Sorry");
        list2.add("Sorry");
        list2.add("sultan");
        list2.add("Masum");
        list2.add("Tanha");
        list2.add("Tarek");
        list2.add("Die");

        int sizeDiffs = CollectionUtils.disjunction(list1, list2).size();
        System.out.println("Diffs: " + sizeDiffs);
    }

    public static void stringCheckOneLiner() {
        boolean exists = false;
        String input = "Q";
        String matcher = "qwert";
        while (StringUtils.containsAny(input.toUpperCase(), matcher.toUpperCase().toCharArray())) {
            exists = true;
            break;
        }
        System.out.println("exists --> " + exists);
    }

    public static void launchGuava() {
        List<BaseDTO> list = GuavaFluentIterable.loadMockData();
        GuavaFluentIterable.applyReduce21(list);
    }

    public static void launchReplaceFrom() {
        String replaceFrom = CharMatcher.anyOf("A").replaceFrom("HOLA", "I");
        System.out.println("GUAVA replaceFrom " + replaceFrom);

        Optional<StringBuffer> bf = FluentIterable.from(MyReplaceFrom.getReplaceParameters("A", "HOLA", "I"))
                .transform(new MyReplaceFrom())
                .first();
        System.out.println("GUAVA function custom " + bf.get());

        StringBuffer sw = new StringWriter() {
            {
                write(StringUtils.replaceEach("HOLA", new String[]{"A"}, new String[]{"I"}));
            }
        }.getBuffer();
        System.out.println("StringBuffer getBuffer1 " + sw);
    }

    public static void launchSortingVector() {
        Vector vector = new Vector() {
            {
                add(new Vector() {
                    {
                        add("KEYA");
                        add(100);
                        add(1.1);
                    }
                });
                add(new Vector() {
                    {
                        add("KEYC");
                        add(11);
                        add(2.2);
                    }
                });
                add(new Vector() {
                    {
                        add("KEYB");
                        add(10);
                        add(3.3);
                    }
                });
            }
        };

        System.out.println("Sorted by KEY");
        Collections.sort(vector, COMPARATOR_BY_KEY);
        for (Object v : vector) {
            System.out.println(v);
        }

        System.out.println("Sorted by Column2");
        Collections.sort(vector, COMPARATOR_BY_OTHER_PROP);
        for (Object v : vector) {
            System.out.println(v);
        }

    }
    public static final Comparator<Vector> COMPARATOR_BY_KEY = new Comparator<Vector>() {
        public int compare(Vector o1, Vector o2) {
            String elementAt1 = (String) o1.elementAt(0);
            String elementAt2 = (String) o2.elementAt(0);
            return elementAt1.compareTo(elementAt2);
        }
    };
    public static final Comparator<Vector> COMPARATOR_BY_OTHER_PROP = new Comparator<Vector>() {
        public int compare(Vector o1, Vector o2) {
            Integer elementAt1 = (Integer) o1.elementAt(1);
            Integer elementAt2 = (Integer) o2.elementAt(1);
            return elementAt1.compareTo(elementAt2);
        }
    };

    public static class Type {

        private String name;
        private int value;

        public Type(String name) {
            this.name = name;
        }

        public Type(int value) {
            this.value = value;
        }

        public String getName() {
            return this.name;
        }

        public int getValue() {
            return this.value;
        }

        public <T> T getDynamicValue(Type t) {
            if (isInt()) {
                return (T) ((Integer) t.getValue());
            } else {
                return (T) t.getName();
            }
        }

        public boolean isInt() {
            boolean isInt = false;
            if (this.value != 0) {
                isInt = true;
                return isInt;
            }
            return isInt;

        }
    }
}
